class UserModel {
  final String id;                       // 用户ID
  final String username;                 // 用户名
  final String nickname;                 // 昵称
  final int gender;                      // 性别（0-未知，1-男，2-女）
  final String? email;                   // 邮箱
  final String? phone;                   // 手机
  final String? avatar;                  // 头像
  final String description;              // 个人描述
  final String pwdResetTime;             // 密码重置时间
  final bool pwdExpired;                 // 密码是否过期
  final String registrationDate;         // 注册日期
  final String deptId;                   // 部门ID
  final String deptName;                 // 部门名称
  final List<String> permissions;        // 权限列表
  final List<String> roles;              // 角色列表
  final List<StoreModel> stores;         // 店铺列表

  const UserModel({
    this.id = '',
    this.username = '',
    this.nickname = '',
    this.gender = 0,
    this.email,
    this.phone,
    this.avatar,
    this.description = '',
    this.pwdResetTime = '',
    this.pwdExpired = false,
    this.registrationDate = '',
    this.deptId = '',
    this.deptName = '',
    this.permissions = const [],
    this.roles = const [],
    this.stores = const [],
  });

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['id'] ?? '',
      username: json['username'] ?? '',
      nickname: json['nickname'] ?? '',
      gender: json['gender'] ?? 0,
      email: json['email'],
      phone: json['phone'],
      avatar: json['avatar'],
      description: json['description'] ?? '',
      pwdResetTime: json['pwdResetTime'] ?? '',
      pwdExpired: json['pwdExpired'] ?? false,
      registrationDate: json['registrationDate'] ?? '',
      deptId: json['deptId'] ?? '',
      deptName: json['deptName'] ?? '',
      permissions: List<String>.from(json['permissions'] ?? []),
      roles: List<String>.from(json['roles'] ?? []),
      stores: (json['stores'] as List? ?? [])
          .map((e) => StoreModel.fromJson(e))
          .toList(),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'nickname': nickname,
      'gender': gender,
      'email': email,
      'phone': phone,
      'avatar': avatar,
      'description': description,
      'pwdResetTime': pwdResetTime,
      'pwdExpired': pwdExpired,
      'registrationDate': registrationDate,
      'deptId': deptId,
      'deptName': deptName,
      'permissions': permissions,
      'roles': roles,
      'stores': stores.map((e) => e.toJson()).toList(),
    };
  }
}

class StoreModel {
  final int storeId;                     // 店铺ID
  final String storeName;               // 店铺名称
  final String storeAddress;            // 店铺地址
  final String logoUrl;                 // 店铺Logo链接
  final String owner;                   // 店主姓名
  final String contact;                 // 联系方式
  final List<String> roles;             // 角色列表
  final String userTypeDesc;            // 用户类型描述
  final bool isPrimary;                 // 是否主店铺

  const StoreModel({
    this.storeId = 0,
    this.storeName = '',
    this.storeAddress = '',
    this.logoUrl = '',
    this.owner = '',
    this.contact = '',
    this.roles = const [],
    this.userTypeDesc = '',
    this.isPrimary = false,
  });

  factory StoreModel.fromJson(Map<String, dynamic> json) {
    return StoreModel(
      storeId: json['storeId'] ?? 0,
      storeName: json['storeName'] ?? '',
      storeAddress: json['storeAddress'] ?? '',
      logoUrl: json['logoUrl'] ?? '',
      owner: json['owner'] ?? '',
      contact: json['contact'] ?? '',
      roles: List<String>.from(json['roles'] ?? []),
      userTypeDesc: json['userTypeDesc'] ?? '',
      isPrimary: json['isPrimary'] ?? false,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'storeId': storeId,
      'storeName': storeName,
      'storeAddress': storeAddress,
      'logoUrl': logoUrl,
      'owner': owner,
      'contact': contact,
      'roles': roles,
      'userTypeDesc': userTypeDesc,
      'isPrimary': isPrimary,
    };
  }
}